﻿using System.Drawing;
using DC2010;
using DC2010.Objects.Data;
using DC2010.Objects.Common;

namespace DC2012
{
    public partial class CoreEditor
    {
        private int mapOriginX = 210;
        private int mapOriginY = 10;

        private void ConstructMap()
        {
            FloorInfo floor = Root.I.FloorSystem.GetInfo(ActiveMap);
            foreach (KeyValue kv in floor.TileLayer.tableOfObjects)
            {
                string[] sfield = kv.Key.Split('_');
                int X = sfield[0].ToInt();
                int Y = sfield[1].ToInt();

                TileInfo ti = Root.I.TileSystem.GetInfo(ActiveMap+"_"+X+"_"+Y);

                if (ti != null)
                {
                    switch (ti.TileType)
                    {
                        case TileType.Wall:
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                break;
                            }

                        case TileType.Stairs:
                            {
                                if (ti.IsUp)
                                    Helper_DisplayTile(X, Y, kv.Value, 3, "TILE_STAIRS_UP", true, ti);
                                else
                                    Helper_DisplayTile(X, Y, kv.Value, 12, "TILE_STAIRS_DOWN", true, ti);
                                break;
                            }
                        case TileType.WallDecorationFountain:
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 7, "TILE_FOUNTAIN", true, ti);
                                break;
                            }
                        case TileType.Floor: // "FLOOR":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 31, "TILE_FLOOR", false, ti);
                                break;
                            }
                        case TileType.Pit: // "PIT":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 1, "TILE_PIT", false, ti);
                                break;
                            }
                            //case TileType.u "UNK":
                            //    {
                            //        Helper_DisplayTile(X, Y, kv.Value, 2, "UNK_", false);
                            //        break;
                            //    }
                        case TileType.Teleport: // "TELEPORT":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 13, "TILE_TELEPORT", false, ti);
                                break;
                            }
                        case TileType.PressPadSmall: // "PRESSURE_PAD_SMALL":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 14, "TILE_PRESSURE_PAD_SMALL", false, ti);
                                break;
                            }
                        case TileType.PressPadBig: // "PRESSURE_PAD_BIG":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 31, "TILE_FLOOR", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 10, "TILE_PRESSURE_PAD_BIG", false, ti);
                                break;
                            }
                        case TileType.IllusionWall: // "ILLUSION_WALL":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 18, "TILE_ILLUSION_WALL", false, ti);
                                break;
                            }
                        case TileType.TurnLeft: //"TURN_LEFT":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 15, "TILE_TURN_LEFT", true, ti);
                                break;
                            }
                        case TileType.TurnRight: // "TURN_RIGHT":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 16, "TILE_TURN_RIGHT", true, ti);
                                break;
                            }
                        case TileType.TurnOposite: // "TURN_OPPOSITE":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 17, "TILE_TURN_OPPOSITE", false, ti);
                                break;
                            }
                        case TileType.Generator: // "GENERATOR":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 31, "TILE_FLOOR", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 22, "TILE_GENERATOR", false, ti);
                                break;
                            }
                        case TileType.Blocker: // "BLOCKER":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 25, "TILE_BLOCKER", false, ti);
                                break;
                            }
                        case TileType.Grate: // "GRATE_SQUARE":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 24, "TILE_GRATE_SQUARE", false, ti);
                                break;
                            }
                            //case "GRATE_CIRCLE":
                            //    {
                            //        Helper_DisplayTile(X, Y, kv.Value, 26, "GRATE_CIRCLE_", false);
                            //        break;
                            //    }
                            //case "GATE_BUTTONS":
                            //    {
                            //        Helper_DisplayTile(X, Y, kv.Value, 31, "FLOOR_", false);
                            //        Helper_DisplayTile(X, Y, kv.Value, 4, "GATE_BUTTONS_", true);
                            //        break;
                            //    }
                        case TileType.Door: //"WOODEN_GATE":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 31, "TILE_FLOOR", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 5, "TILE_WOODEN_GATE", true, ti);
                                break;
                            }
                            //case TileType.Door:// "GATE":
                            //    {
                            //        Helper_DisplayTile(X, Y, kv.Value, 31, "FLOOR_",false);
                            //        Helper_DisplayTile(X, Y, kv.Value, 6, "GATE_",true);
                            //        break;
                            //    }
                        case TileType.Button: // "BUTTON":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 21, "TILE_BUTTON", true, ti);
                                break;
                            }
                        case TileType.WallDecoration: // "DECORATION":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 9, "TILE_DECORATION", true, ti);
                                break;
                            }
                        case TileType.Alcove: // "ALCOVE":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 19, "TILE_ALCOVE", true, ti);
                                break;
                            }
                        case TileType.WallDecorationSmall: // "DECORATION_SMALL":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 20, "TILE_DECORATION_SMALL", true, ti);
                                break;
                            }
                            //case "MIRROR":
                            //    {
                            //        Helper_DisplayTile(X, Y, kv.Value, 0, "WALL_", false);
                            //        Helper_DisplayTile(X, Y, kv.Value, 21, "MIRROR_", true);
                            //        break;
                            //    }
                            //case "BUTTON_GREEN":
                            //    {
                            //        Helper_DisplayTile(X, Y, kv.Value, 0, "WALL_", false);
                            //        Helper_DisplayTile(X, Y, kv.Value, 27, "BUTTON_GREEN_",true);
                            //        break;
                            //    }
                        case TileType.FlameThrower:
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 28, "TILE_FIRE_THROWER", true, ti);
                                break;
                            }
                        case TileType.Ressurection:
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 0, "TILE_WALL", false, ti);
                                Helper_DisplayTile(X, Y, kv.Value, 30, "TILE_RESURRECTION", true, ti);
                                break;
                            }
                        case TileType.SwitchWall: // "SWITCH_WALL":
                            {
                                Helper_DisplayTile(X, Y, kv.Value, 11, "TILE_SWITCH_WALL", false, ti);
                                break;
                            }
                    }
                }
            }
        }

        public void Helper_DisplayTile(int X, int Y, string cd, int spriteIndex, string idPrefix, bool allowRotate, TileInfo ti)
        {
            //bool isNorth = cd.Contains("_NORTH");
            //bool isWest = cd.Contains("_WEST");
            //bool isEast = cd.Contains("_EAST");

            DCAnimatedSprite sx = Neocortex.I.GetSubFrame("TILESET", idPrefix+"_"+X+"_"+Y, spriteIndex);
            //DCAnimatedSprite sx = Neocortex.I.GetAnimatedSprite(idPrefix);

            if (allowRotate)
            {
                if (ti.Direction == Directions.East) sx.Surface = sx.Surface.CreateRotatedSurface(90);
                if (ti.Direction == Directions.North) sx.Surface = sx.Surface.CreateRotatedSurface(180);
                if (ti.Direction == Directions.West) sx.Surface = sx.Surface.CreateRotatedSurface(270);
            }

            sx.X = mapOriginX + X * 16;
            sx.Y = mapOriginY + Y * 16;
            //Point p = GetMapCoordinates(sx.X, sx.Y);

            //sx.Id = idPrefix + +p.X + "," + p.Y;
            AddSprite(sx, map);
        }
    }
}
